import {
  addressAdd,
  addressUpdate,
  addressInfo,
  listAddress

} from "../../../http/api"
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    name: "",
    address: "",
    city: "",
    phone: "",
    communityName:"",
    community: "",
    isDefault: true,
    columns: ['昌邑区', '龙潭区', '船营区', '丰满区', '永吉县', '蛟河市', '桦甸市', '舒兰市', '磐石市'],
    communityArray:[],
    communityPickArray:[],
    lon:"",
    lat:"",


  },

  handleChangeCity(e) {
    const index = e.detail.value
    this.setData({
      city: this.data.columns[index]
    })
  },

  handleSearch(){
 
    wx.showLoading()
    listAddress({community:this.data.community}).then(res => {
      if (res.data.code === 200) {
        const communityPickArray = res.data.data.map((item)=>{
          return item.address
        })
        this.setData({
          communityArray:[...res.data.data],
          communityPickArray
        })
        
      } 
    }).finally(() => {
      wx.hideLoading()
    })

  },

  handleChangeCommunity(e) {
    const index = e.detail.value
    this.setData({
      city: this.data.communityPickArray[index],
      lat:this.data.communityArray[index].location.lat,
      lon:this.data.communityArray[index].location.lng,
    })

  },

  handleChangeDefault({
    detail
  }) {
    this.setData({
      isDefault: detail
    });
  },

  handleSubmit() {
    if (this.data.id) {
      this.handleUpdate()
    } else {
      this.handleAdd()
    }

  },


  handleAdd() {
    const {
      name,
      address,
      city,
      phone,
      community,
      isDefault,
      lat,
      lon
    } = this.data

    if (!this.isValidPhoneNumber(phone)) {
      
      wx.showToast({
        title: '电话号不正确',
        icon: 'error'
      })
      return
    }
    const params = {
      lat,
      lon,
      name,
      address,
      city,
      phone,
      community,
      isDefault: isDefault ? '1' : '0',
      customerId: wx.getStorageSync('id')
    }
    wx.showLoading()
    addressAdd(params).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      } else {

        wx.showModal({
          title: '提示',
          content: res.data.msg,
          showCancel:false
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })

  },

  isValidPhoneNumber(phoneNumber) {
    var pattern = /^1[3456789]\d{9}$/;
    return pattern.test(phoneNumber);
  },



  handleUpdate() {
    const {
      id,
      name,
      address,
      city,
      phone,
      community,
      isDefault
    } = this.data

    if (!this.isValidPhoneNumber(phone)) {
      wx.showToast({
        title: '电话号不正确',
        icon: 'error'
      })
      return
    }
    const params = {
      id,
      name,
      address,
      city,
      phone,
      community,
      isDefault: isDefault ? '1' : '0',
      customerId: wx.getStorageSync('id')
    }
    wx.showLoading()
    addressUpdate(params).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.msg,
          showCancel:false
        })

      }
    }).finally(() => {
      wx.hideLoading()
    })
  },
  getData() {


    wx.showLoading()
    addressInfo(this.data.id).then(res => {
      if (res.data.code === 200) {
        const {
          id,
          name,
          address,
          city,
          phone,
          community,
          isDefault
        } = res.data.data
        this.setData({
          id,
          name,
          address,
          city,
          phone,
          community,
          isDefault: isDefault == 1 ? true : false
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      id
    } = options

    if (id) {
      this.setData({
        id
      })
      this.getData()
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})